﻿using System;
using System.Collections.Generic;
using System.Linq;
using DNT.RavenQA.Core.Extensions;

namespace DNT.RavenQA.Core.Models
{
    public class ContributeViews : Entity<Int32>
    {
        private IList<String> _users = new List<String>();

        protected ContributeViews()
        {
        }

        public static ContributeViews New(Question question)
        {
            question.EnsureIsNotNull("question");
            return new ContributeViews { QuestionId = question.Id };
        }

        internal void New(User user)
        {
            user.EnsureIsNotNull("user");

            if (HasAlreadyViewed(user))
                return;

            _users.Add(user.Id);
        }

        public String QuestionId { get; private set; }

        protected IList<String> Users
        {
            get { return _users; }
            private set { _users = value; }
        }

        public Int32 TotalAmount
        {
            get { return Users.Count(); }
        }

        private Boolean HasAlreadyViewed(User user)
        {
            user.EnsureIsNotNull("user");
            return Users.SingleOrDefault(x => x == user.Id).IsNotNull();
        }
    }
}